<?php
namespace app\farm\model;

use app\BaseModel;
use app\farm\server\Land;
use longbingcore\wxcore\PospalApi;
use think\facade\Db;

class Machine extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_machine';


    protected $append = [

        'farmer_name'

    ];


    /**
     * @author chenniang
     * @DataTime: 2022-02-21 16:58
     * @功能说明:农场主名字
     */
    public function getFarmerNameAttr($value,$data){

        if(!empty($data['farmer_id'])){

            $dis = [

                'id' => $data['farmer_id'],

            ];

            $farmer_model = new Farmer();

            $name = $farmer_model->where($dis)->where('status','in',[2,3])->value('title');

            return $name;

        }


    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis,$file='*'){

        $data = $this->where($dis)->field($file)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-21 17:58
     * @功能说明:远程获取仪器的数据
     */
    public function getDataInfo($land_id,$type=1){

        $model = $type==1?new LandList():new Claim();

        $id = $model->where(['id'=>$land_id])->value('machine_id');

        $data = $this->dataInfo(['id'=>$id]);

        if(empty($data)){

            return [];
        }

        $mac_model  = new PospalApi();

        $room = $mac_model->getRoomInfo($data['room_id']);

        if($room['result']!='success'){

            return [];
        }

        $server_time = $room['server_time'];

        foreach ($room['channel'] as $key=>$value){

            if(is_string($value)&&strpos($key,'field') !== false){

                $arr[$key]['field'] = $key;

                $arr[$key]['text'] = $value;

            }

        }

        $last_value = json_decode($room['channel']['last_values'],true);

        if(!empty($arr)){

            foreach ($arr as $k=>$v){

                $arr[$k]['value'] = isset($last_value[$v['field']])?$last_value[$v['field']]['value']:'';
            }
        }

        $list['server_time'] = $server_time;

        $list['data'] = array_values(array_values($arr));

        return $list;
    }






}